var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: '',
    //显示数据
    appointmentInfo: [],
    scrollHeight: 0,
    currentId: -1,
    appointmentInfo1: [{
      logoLink: '/images/defaultImage.png',
      patientName: '小红',
      orderDate: '2020-11-31',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小红',
      orderDate: '2020-12-11',
      orderTime: '下午',
      patientTel: "18727508188"
    },
    ],
    patientTel: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();
    console.log(1)
    console.log(options)
    this.setData({
      patientTel: JSON.parse(options.queryBean),
      username: getApp().globalData.username,
    })
    console.log(this.data.patientTel)
    
    this.getPatientAppointment()
    try {
      var ap = wx.getStorageSync('appointmentInfo')
      this.setData({
        appointmentInfo: ap
      })
    } catch(e) { }

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPatientAppointment()

    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('appointmentInfo')
      this.setData({
        appointmentInfo: ap
      })
    } catch(e) { }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getPatient()
  },
  
  /**
   * 获取病人信息
   */
  getPatientAppointment: function () {
    var that = this
    var id = this.data.username
    var patientTel = this.data.patientTel
    console.log(id)
    console.log(patientTel)
    wx.request({
      url: this.data.url + 'getPatientAppointmentInfo',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorTel: id,
        patientTel: patientTel
      },
      success: function (res) {
        console.log(res.data)
        if (res.data != false && res.data != "False" && res.data != "空") {
          try {
            wx.setStorageSync("appointmentInfo", res.data)
          } catch (e) { }
          that.setData({
            appointmentInfo: res.data
          })
          console.log(that.data.appointmentInfo)
        }
        else if(res.data == "空" || res.data == "False") {
          that.setData({
            patient: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 查看病人病历
   */
  medicalRecord: function(e){
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(that.data.appointmentInfo[index].patientTel)
    var orderDate = JSON.stringify(that.data.appointmentInfo[index].orderDate)
    var orderTime = JSON.stringify(that.data.appointmentInfo[index].orderTime)
    wx.navigateTo({
      url: '../viewMedicalRecord/viewMedicalRecord?patientTel=' + patientTel + '&&orderDate=' + orderDate + '&&orderTime=' + orderTime,
    })
  },

  /**
   * 跳转病人信息
   */
  clickPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(that.data.appointmentInfo[index].patientTel)
    var patientOrderDate = JSON.stringify(that.data.appointmentInfo[index].orderDate)
    var patientName = JSON.stringify(that.data.appointmentInfo[index].patientName)
    wx.navigateTo({
      url: '../patientMessage/patientMessage?patientTel=' + patientTel + '&&patientOrderDate=' + patientOrderDate + '&&name=' + patientName,
    })
  }
})